import { Service } from 'egg'

type PageOption = {
  page?: number
  pageSize?: number
}
export default class Order extends Service {
  // 关联查询 
  public async getOrderList(option: PageOption) {
    const { page = 1, pageSize = 10 } = option || {}
    const { ctx } = this
    // ctx.model.User.findCreateFind
    const { Order, User, Product } = ctx.model
    const datas = await Order.findAndCountAll<any>({
      attributes: { exclude: ['createdAt'] },
      include: [
        {
          model: User,
          as: 'user_info',
          attributes: { exclude: ['password', 'createdAt'] },
        },
        { model: Product, attributes: { exclude: ['createdAt'] } },
      ],
      limit: pageSize, // 一页多少条数据
      offset: (page - 1) * pageSize, // 从哪个下标开始查询，就是当前页数了
    })
    return datas
  }
}
